package cn.invest.merry.module.invest.controller.admin.gsbase;

import cn.invest.merry.framework.apilog.core.annotation.ApiAccessLog;
import cn.invest.merry.framework.common.pojo.CommonResult;
import cn.invest.merry.framework.common.pojo.PageParam;
import cn.invest.merry.framework.common.pojo.PageResult;
import cn.invest.merry.framework.common.util.object.BeanUtils;
import cn.invest.merry.framework.excel.core.util.ExcelUtils;
import cn.invest.merry.module.invest.controller.admin.gsbase.vo.GsBasePageReqVO;
import cn.invest.merry.module.invest.controller.admin.gsbase.vo.GsBaseRespVO;
import cn.invest.merry.module.invest.controller.admin.gsbase.vo.GsBaseSaveReqVO;
import cn.invest.merry.module.invest.dal.dataobject.gsbase.GsBaseDO;
import cn.invest.merry.module.invest.service.gsbase.GsBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.invest.merry.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.invest.merry.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 公司基本信息")
@RestController
@RequestMapping("/invest/gs-base")
@Validated
public class GsBaseController {

    @Resource
    private GsBaseService gsBaseService;

    @PostMapping("/create")
    @Operation(summary = "创建公司基本信息")
    @PreAuthorize("@ss.hasPermission('invest:gs-base:create')")
    public CommonResult<String> createGsBase(@Valid @RequestBody GsBaseSaveReqVO createReqVO) {
        return success(gsBaseService.createGsBase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司基本信息")
    @PreAuthorize("@ss.hasPermission('invest:gs-base:update')")
    public CommonResult<Boolean> updateGsBase(@Valid @RequestBody GsBaseSaveReqVO updateReqVO) {
        gsBaseService.updateGsBase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('invest:gs-base:delete')")
    public CommonResult<Boolean> deleteGsBase(@RequestParam("id") String id) {
        gsBaseService.deleteGsBase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('invest:gs-base:query')")
    public CommonResult<GsBaseRespVO> getGsBase(@RequestParam("id") String id) {
        GsBaseDO gsBase = gsBaseService.getGsBase(id);
        return success(BeanUtils.toBean(gsBase, GsBaseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司基本信息分页")
    @PreAuthorize("@ss.hasPermission('invest:gs-base:query')")
    public CommonResult<PageResult<GsBaseRespVO>> getGsBasePage(@Valid GsBasePageReqVO pageReqVO) {
        PageResult<GsBaseDO> pageResult = gsBaseService.getGsBasePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GsBaseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('invest:gs-base:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGsBaseExcel(@Valid GsBasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GsBaseDO> list = gsBaseService.getGsBasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司基本信息.xls", "数据", GsBaseRespVO.class,
                        BeanUtils.toBean(list, GsBaseRespVO.class));
    }

}